package com.ruoyi.report.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.ReportOrginalrecord;
import com.ruoyi.report.service.IReportOrginalrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 原始记录信息Controller
 * 
 * @author luguanqing
 * @date 2022-03-10
 */
@RestController
@RequestMapping("/report/orginalrecord")
public class ReportOrginalrecordController extends BaseController
{
    @Autowired
    private IReportOrginalrecordService reportOrginalrecordService;

    /**
     * 查询原始记录信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ReportOrginalrecord reportOrginalrecord)
    {
        startPage();
        List<ReportOrginalrecord> list = reportOrginalrecordService.selectReportOrginalrecordList(reportOrginalrecord);
        return getDataTable(list);
    }

    /**
     * 导出原始记录信息列表
     */
    @Log(title = "原始记录信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ReportOrginalrecord reportOrginalrecord)
    {
        List<ReportOrginalrecord> list = reportOrginalrecordService.selectReportOrginalrecordList(reportOrginalrecord);
        ExcelUtil<ReportOrginalrecord> util = new ExcelUtil<ReportOrginalrecord>(ReportOrginalrecord.class);
        return util.exportExcel(list, "orginalrecord");
    }

    /**
     * 获取原始记录信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(reportOrginalrecordService.selectReportOrginalrecordById(id));
    }

    /**
     * 原始记录图片上传
     */
    @Log(title = "原始记录图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult uploadReport(MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String fileName = FileUploadUtils.upload(RuoYiConfig.getOrginalPath(), file, true);
            String profile =  RuoYiConfig.getProfile();
            if(StringUtils.isNotEmpty(fileName)){
                return AjaxResult.success("上传成功", StringUtils.replace(fileName, profile, "/profile"));
            }
        }
        return AjaxResult.error("上传文件异常，请联系管理员");

    }

    /**
     * 新增原始记录信息
     */
    @Log(title = "原始记录信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportOrginalrecord reportOrginalrecord)
    {
        return toAjax(reportOrginalrecordService.insertReportOrginalrecord(reportOrginalrecord));
    }


    /**
     * 修改原始记录信息
     */
    @Log(title = "原始记录信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportOrginalrecord reportOrginalrecord)
    {
        reportOrginalrecord.setChanger(SecurityUtils.getLoginUser().getUser().getNickName());
        reportOrginalrecord.setChangeTime(DateUtils.getNowDate());
        return toAjax(reportOrginalrecordService.updateReportOrginalrecord(reportOrginalrecord));
    }

    /**
     * 删除原始记录信息
     */
    @Log(title = "原始记录信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(reportOrginalrecordService.deleteReportOrginalrecordByIds(ids));
    }
}
